package com.example.ibsp_demo.service.impl;

import com.example.ibsp_demo.mapper.admin.ActivityMapper;
import com.example.ibsp_demo.mapper.content.ContentMapper;
import com.example.ibsp_demo.mapper.log.AdminOperationLogMapper;
import com.example.ibsp_demo.mapper.message.SendBackMapper;
import com.example.ibsp_demo.pojo.entity.admin.Activity;
import com.example.ibsp_demo.pojo.entity.content.Content;
import com.example.ibsp_demo.pojo.entity.content.Partition;
import com.example.ibsp_demo.pojo.entity.message.SendBack;
import com.example.ibsp_demo.pojo.entity.message.UserFeedback;
import com.example.ibsp_demo.pojo.entity.user.User;
import com.example.ibsp_demo.mapper.content.PartitionMapper;
import com.example.ibsp_demo.mapper.message.UserFeedbackMapper;
import com.example.ibsp_demo.mapper.user.UserMapper;
import com.example.ibsp_demo.pojo.log.AdminOperationLog;
import com.example.ibsp_demo.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ContentMapper contentMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private PartitionMapper partitionMapper;

    @Autowired
    private UserFeedbackMapper feedbackMapper;

    @Autowired
    private SendBackMapper sendBackMapper;

    @Autowired
    private AdminOperationLogMapper logMapper;

    @Override
    public List<User> queryUsers(Integer uid) {
        return userMapper.selectUsersByConditions(uid);
    }

    @Override
    public List<User> allUsers() {
        return userMapper.selectAll();
    }

    @Override
    public void unBanUser(Integer uid, Integer aid) {
        userMapper.updateUserStatus(uid, 1); // 状态1表示正常
        logMapper.insert(new AdminOperationLog(null, aid, "解封用户", uid, LocalDateTime.now(), "用户被解封"));
    }

    @Override
    public void banUser(Integer uid, Integer aid) {
        userMapper.updateUserStatus(uid, 2); // 状态2表示封禁
        logMapper.insert(new AdminOperationLog(null, aid, "封禁用户", uid, LocalDateTime.now(), "用户被封禁"));
    }

    @Override
    public List<Content> allContent() {
        return contentMapper.selectAll();
    }

    @Override
    public List<Content> queryContent(Integer ctid) {
        return contentMapper.selectBlogsByConditions(ctid);
    }

    @Override
    public List<Activity> allActivity() {
        return activityMapper.selectAll();
    }

    @Override
    public void publishActivity(Activity activity) {
        activityMapper.addActivity(activity);
    }

    @Override
    public void deleteActivity(Integer id) {
        activityMapper.deleteActivityById(id);
    }

    @Transactional
    @Override
    public void reviewBlog(SendBack sendBack) {
        sendBackMapper.insert(sendBack);
        contentMapper.updateBlogStatus(sendBack.getBlogId(), 2);
        logMapper.insert(new AdminOperationLog(null, 1, "驳回博客", sendBack.getBlogId(), LocalDateTime.now(), "博客被驳回"));
    }

    @Override
    public List<Partition> allPartition() {
        return partitionMapper.selectAllOrderedBySort();
    }

    @Override
    public void addPartition(Partition partition) {
        partitionMapper.insert(partition);
    }

    @Override
    public void updatePartition(Partition partition) {
        partitionMapper.update(partition);
    }

    @Override
    public void deletePartition(Integer pid) {
        partitionMapper.deleteByPid(pid);
    }

    @Override
    public List<UserFeedback> getAllFeedbacks() {
        return feedbackMapper.selectAllFeedbacks();
    }

    @Override
    public void goContent(Integer ctid) {
        contentMapper.updateBlogStatus(ctid, 1);
    }
}